package com.anbinh.doctor.adapter;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.FragmentMain;
import com.anbinh.doctor.fragment.lichhen.FragmentLichhen;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ResponseSchedules.Data.Item;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.zcw.togglebutton.ToggleButton;
import com.zcw.togglebutton.ToggleButton.OnToggleChanged;

public class LichkhamAdapter extends ArrayAdapter<Item> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<Item> listData;
	private Context mContext;
	private UserLogin userLogin;
	private IRcallBak callBak;
	private int doctorId;

	public LichkhamAdapter(Context mContext, int resource, List<Item> models,int doctorId, IRcallBak callBak) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;
		this.callBak=callBak;
		this.doctorId=doctorId;

		layoutInflater = LayoutInflater.from(mContext);
		userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);
		
	}

	public class ViewHolder {
		public ToggleButton toggle;
		public TextView form_time;
		public TextView to_time;
		public TextView address;
		public TextView time_block;
		public View btnSetup;

	}

	@Override
	public Item getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.toggle = (ToggleButton) convertView
					.findViewById(R.id.btnToggle);
			viewHolder.form_time = (TextView) convertView
					.findViewById(R.id.from_time);
			viewHolder.to_time = (TextView) convertView
					.findViewById(R.id.to_time);
			viewHolder.address = (TextView) convertView
					.findViewById(R.id.lblAddress);
			viewHolder.time_block = (TextView) convertView
					.findViewById(R.id.time_block);
			viewHolder.btnSetup = (View) convertView
					.findViewById(R.id.btnSetup);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Item item = getItem(position);
		viewHolder.form_time.setText(item.getTime_from());
		viewHolder.to_time.setText(item.getTime_to());
		viewHolder.address.setText(item.getAddress());
		viewHolder.time_block.setText("Ca: " + item.getTime_slots() + " phút");

		if (item.getIsActive() == 1) {
			viewHolder.toggle.setToggleOn();
		} else {
			viewHolder.toggle.setToggleOff();
		}

		viewHolder.toggle.setOnToggleChanged(new OnToggleChanged() {

			@Override
			public void onToggle(boolean on) {
				if (on) {
					listData.get(position).setIs_active(1);
				} else {
					listData.get(position).setIs_active(0);
				}
				onOff(item);
			}
		});
		if(item.isEdit()){
			viewHolder.btnSetup.setVisibility(View.VISIBLE);
		}else{
			viewHolder.btnSetup.setVisibility(View.GONE);
		}
		viewHolder.btnSetup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				confirmRemove("Bạn có muốn xóa lịch khám này?", item);
			}
		});

		return convertView;
	}
	public void confirmRemove(String message, final Item item) {
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				android.R.color.transparent);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_tamhoan);
		dialog.getWindow().setLayout(width/6*5, LayoutParams.WRAP_CONTENT);
		TextView lblTitle = (TextView) dialog.findViewById(R.id.lblTitle);
		lblTitle.setText(message);
		TextView btnCo = (TextView) dialog.findViewById(R.id.btnCo);

		btnCo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				checkUpdate(item);
			}
		});

		TextView btnKo = (TextView) dialog.findViewById(R.id.btnKo);
		btnKo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();

	}
	public void checkUpdate(final Item item) {
		callBak.onDisplay(Utils.SHOW_PROCESS, null);
		ServerController.getInstance(mContext).requestCheckUpdate(
				userLogin.getData().getToken(), item.getId(), "delete",doctorId,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						if (mContext == null)
							return;
						if (data.getResponsecode() == 201) {
							callBak.onDisplay(Utils.GONE_PROCESS, null);
							showDialogConfirm(data.getDescription(), item);
						} else {
							onDelete(item);
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						callBak.onDisplay(Utils.GONE_PROCESS, null);
					}
				});
	}

	protected void onDelete(final Item item) {
		ServerController.getInstance(mContext).requestDeleteSchedule(
				userLogin.getData().getToken(), item.getId(),doctorId,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						callBak.onDisplay(Utils.GONE_PROCESS, null);
						listData.remove(item);
						notifyDataSetChanged();
						FragmentMain.newInstance().setRefreshAppointMent(true);
					}

					@Override
					public void onError(int errorCode, String message) {
						callBak.onDisplay(Utils.GONE_PROCESS, null);
					}
				});
	}

	public void showDialogConfirm(String message, final Item item) {
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				android.R.color.transparent);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_tamhoan);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);
		TextView lblTitle = (TextView) dialog.findViewById(R.id.lblTitle);
		lblTitle.setText(message);
		TextView btnCo = (TextView) dialog.findViewById(R.id.btnCo);

		btnCo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				onDelete(item);
			}
		});

		TextView btnKo = (TextView) dialog.findViewById(R.id.btnKo);
		btnKo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();

	}

	public void onOff(Item item) {

		ServerController.getInstance(mContext).requestOnOffSchedule(
				userLogin.getData().getToken(), item.getIsActive(),
				item.getId(), new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						FragmentMain.newInstance().setRefreshAppointMent(true);
					}

					@Override
					public void onError(int errorCode, String message) {
						// TODO Auto-generated method stub

					}
				});
	}
}
